package com.example.demo4;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.io.FileExistsException;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;

@WebServlet("/upload")
@MultipartConfig
public class Upload extends HttpServlet {
    public static String UPLOAD_PATH = "";
    public static String FILE_NAME = "";
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        boolean b = ServletFileUpload.isMultipartContent(req);
        if (b){
            DiskFileItemFactory factory = new DiskFileItemFactory();
            ServletContext context = this.getServletConfig().getServletContext();
            //File repo = (File) context.getAttribute("javax.servlet.context.tempdir");
            //这里是设置文件临时的存储路径
            File repo = new File("./upload");
            factory.setRepository(repo);
            ServletFileUpload servletFileUpload = new ServletFileUpload(factory);
            try {
                List<FileItem> fileItems = servletFileUpload.parseRequest(req);
                Iterator<FileItem> iterator = fileItems.iterator();
                while (iterator.hasNext()){
                    FileItem item = iterator.next();
                    if (item.isFormField()) continue;
                    else {
                        //这里是文件在服务器上的存储路径，以下设置方法是在Tomcat的bin路径中
                        File file = new File("uploaded/" + item.getName());
                        int index = file.getAbsolutePath().lastIndexOf('/');
                        UPLOAD_PATH = file.getAbsolutePath().substring(0,index+1);
//                        UPLOAD_PATH = file.getPath();
                        FILE_NAME = item.getName();
                        System.out.println(file.getAbsolutePath());
                        System.out.println(UPLOAD_PATH);
                        item.write(file);
                    }
                }
                req.getRequestDispatcher("success.jsp").forward(req,resp);
//                System.out.println(fileItems);
            }catch (FileExistsException fe){
                System.out.println("file already exists");
                req.getRequestDispatcher("success.jsp").forward(req,resp);
            }catch (Exception e) {
                req.getRequestDispatcher("success.jsp").forward(req,resp);
            }
        }
    }
}
